"use strict";
cc._RF.push(module, '27bbbodE8VDL6D+ZODWmkoK', 'WXAds');
// resources/Script/Ssg/API/WX/WXAds.js

'use strict';

var mBannerAd = null,
    mVideoAd = null,
    wxVideoId = null,
    wxBannerId = null;

var WXAds = cc.Class({

    statics: {

        init: function init(wxBannerId, wxVideoId) {
            this.wxBannerId = wxBannerId;
            this.wxVideoId = wxVideoId;
            mVideoAd = wx.createRewardedVideoAd({
                adUnitId: this.wxVideoId
            });
        },

        /**
         * 播放banner
         */
        showBanner: function showBanner() {
            var screenSize = wx.getSystemInfoSync();
            if (screenSize.SDKVersion < '2.0.4') {
                //不满足显示广告的SDK版本
                return;
            }
            if (mBannerAd) {
                mBannerAd.destory();
            }

            var showWidth = screenSize.screenWidth;
            mBannerAd = wx.createBannerAd({
                adUnitId: GAME_INFO.BANNER_ID,
                style: {
                    left: 0,
                    top: 0,
                    width: showWidth
                }
            });

            mBannerAd.onResize(function (res) {
                var left = (screenSize.screenWidth - res.width) / 2;
                var top = screenSize.screenHeight - res.height;

                // if (UserData.getIsIPhoneX()) {
                //     top -= 20;
                // }
                mBannerAd.style.top = top;
                mBannerAd.style.left = left;
                mBannerAd.show();
            });
        },

        showVide: function showVide(callback) {
            if (!mVideoAd) {
                mVideoAd = wx.createRewardedVideoAd({
                    adUnitId: this.wxVideoId
                });
            }
            mVideoAd.load().then(function () {
                return mVideoAd.show();
            }).catch(function (err) {
                return Log.error(err.errMsg);
            });

            var thisCallback = function thisCallback(res) {
                if (callback) {
                    if (res && res.isEnded || res === undefined) {
                        // 正常播放结束，可以下发游戏奖励
                        callback(true);
                    } else {
                        // 播放中途退出，不下发游戏奖励
                        callback(false);
                    }
                }
                SoundUtil.resumeMusic();
                mVideoAd.offClose(thisCallback);
            };

            mVideoAd.onClose(thisCallback);
            return;
        }

    }
});

module.exports = WXAds;

cc._RF.pop();